<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">Token管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>Token管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">


        <!-- 数据表格 -->
        <table class="layui-table" id="token-table" lay-filter="token-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="token-table-bar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script>

    layui.use(['form', 'table', 'util', 'config', 'admin'],function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        table.render({
            elem: '#token-table',
            url: config.base_server + 'api-auth/oauth/token/list',
            method: 'POST',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'user_id',width:80, sort: true, title: '用户编号'},
                {field: 'user_name',  sort: true, title: '用户名称'},
                {field: 'client_id', sort: true, title: '应用ID'},
                {field: 'token_type', sort: true, title: '类型'},
                {field: 'token_value', sort: true, title: 'token'},
                {field: 'grant_type' , sort: true, title: '授权方式'},
                {
                    field: 'user_head_imgurl', width: 250, align: 'center', templet: function (d) {
                            return '<img src="'+d.user_head_imgurl+'" class="layui-badge layui-bg-gray"></img>';
                    }, title: '用户头像'
                },
                {align: 'center', width:80, toolbar: '#token-table-bar', title: '操作'}
            ]]
        });

        // 工具条点击事件
        table.on('tool(token-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') { // 删除
                doDelete(obj);
            }
        });


        // 删除
        var doDelete = function (obj) {
            layer.confirm('确定要删除吗？', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('api-auth/oauth/remove/token?access_token=' + obj.data.token_value, {}, function (data) {
                    layer.closeAll('loading');
                    layer.msg('成功', {icon: 1, time: 500});
                    obj.del();
                    // if (data.code == 0) {
                    //     layer.msg(data.msg, {icon: 1});
                    //     obj.del();
                    // } else {
                    //     layer.msg(data.msg, {icon: 2});
                    // }
                }, 'POST');
            });
        };


    });

</script>